package com.paic.mhis.hcpms.quota.exportword.itext;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.paic.mhis.core.exception.FwaBusinessException;
import com.paic.mhis.hcpms.common.community.dto.MedicalOrgInfoDTO;
import com.paic.mhis.hcpms.quota.exportword.dto.KpiBatchDTO;
import com.paic.mhis.hcpms.quota.exportword.utils.GeneralMethods;

/**  
 * 创建table工具类 
 * @author:     EX-XIONGFEI002
 * @version:    
 */
public class CreateRtfTableDataHelper {

	private static final Logger LOGGER = Logger.getLogger(CreateRtfTableDataHelper.class);

	// template的存放目录
	public static final String TEMPLEATE_DIR = "itext/templates/";

	// images的存放目录
	public static final String IMAGES_DIR = "itext/images/";

	// 缓存目录
	public static final String TEMP_DIR = "itext/temp/temp.rtf";

	// itext/images
	public static final String ITEXT_IMAGES_DIR = "itext/images/";

	/**
	 * CreateRtfTableDataHelper    
	 */
	private CreateRtfTableDataHelper() {
	}

	private static CreateRtfTableDataHelper instance = new CreateRtfTableDataHelper();

	public static CreateRtfTableDataHelper getInstance() {
		return instance;
	}

	/**
	* rtf的缓存文件路径
	* @return 缓存文件路径
	*/
	private String getTmpRtfPath(String reportTemplateDir) {
		return reportTemplateDir + CreateRtfTableDataHelper.TEMP_DIR;
	}

	/**
	* rtf的缓存文件路径
	* @return 缓存文件路径
	*/
	public String getItextImagesPath(String reportTemplateDir) {
		return reportTemplateDir + CreateRtfTableDataHelper.ITEXT_IMAGES_DIR;
	}

	/**
	 * 
	 * @param regionCode			regionCode
	 * @param ogrCode					ogrId
	 * @return	图片的存放地址
	 * @throws FwaBusinessException
	 */
	public String getImagesDir(Map<String, Object> paramMap, String rtfReportDir) throws FwaBusinessException {

		String rtfFileDir = rtfReportDir + paramMap.get("regionCode") + '/' + paramMap.get("currentDate") + '/';

		if (StringUtils.hasText((String) paramMap.get("medicalOrgCode"))) {
			rtfFileDir += (String) paramMap.get("medicalOrgCode") + '/';
		}

		File file = new File(rtfFileDir);

		if (!file.exists()) {
			if (!file.mkdirs()) {
				LOGGER.error("create file path is failed.");
				throw new FwaBusinessException("创建images存放路径失败");
			}
		}

		return rtfFileDir;
	}

	/**
	 * 创建rtf文件的输出目录
	 * @return 创建存放word的目录
	 * @throws Exception
	 */
	public String createRtfOutputFileDir(KpiBatchDTO kpiBatchDTO, MedicalOrgInfoDTO medicalOrgInfoDTO,
			String rtfReportDir) throws FwaBusinessException {
		String rtfFileDir = rtfReportDir + kpiBatchDTO.getRegionCode() + '/' + kpiBatchDTO.getKpiTime() + '/';

		if (null != medicalOrgInfoDTO) {
			rtfFileDir += medicalOrgInfoDTO.getMedicalOrgCode() + "/";
		}

		File file = new File(rtfFileDir);

		if (!file.exists()) {
			if (!file.mkdirs()) {
				LOGGER.error("create rtfFileDir is fail.");
				throw new FwaBusinessException("创建word存放路径失败");
			}
		}

		return rtfFileDir;
	}

	/**
	 * 平均床日费和每床日费用
	 * @param tableDataList  tableDataList
	 * @param titleNames     表头
	 * @return table
	 */
	public Table createAvgHospAndAvgBedTable(List<String[]> tableDataList, String[] titleNames) {
		Table table = createAvgHospAndAvgBedTableHeader(titleNames);
		createAvgHospAndAvgBedTableBody(tableDataList, table);

		return table;

	}

	/**
	* 医疗总费用和医保占比table
	* @param tableDataList tableDataList
	* @return table
	*/
	public Table crateTotFeeAndMedFeeRateTable(List<String[]> tableDataList) {
		Table table = createTotFeeAndMedFeeRateTableHeader("1.1医疗总费用 (万元)");

		createTotFeeAndMedFeeRateTableBody(tableDataList, table);

		return table;
	}

	/**
	 * 杨理刚
	 * 第二部分：住院---住院费用和医保费用占比
	 * 第二部分：门诊---门诊费用和医保费用占比
	 * @param tableDataList tableDataList
	 * @param titleName 表头名称
	 * @return
	 */
	public Table crateHostFeeAndMedRateTable(List<String[]> tableDataList, String titleName) {
		Table table = createTotFeeAndMedFeeRateTableHeader(titleName);

		createTotFeeAndMedFeeRateTableBody(tableDataList, table);

		return table;
	}

	/**
	* 各科室对比
	* @param tableDataList tableDataList
	* @return table
	*/
	public Table crateWordDeptsContrasTable(List<String[]> tableDataList) {
		Table table = createWordDeptsContrasTableHeader();

		createWordDeptsContrasTableBody(tableDataList, table);

		return table;
	}

	/**
	* 门诊:药品费用和其他费用---各机构对比
	* 住院:药品费用和其他费用---各机构对比
	* @param tableDataList tableDataList
	* @return table
	 * @throws BadElementException 
	*/
	public Table crateDrugFeeAndOtherFeeRateTable1(List<String[]> tableDataList, String[] titleNames) {
		Table table = null;
		try {
			table = createDrugFeeAndOtherFeeRateTableHeader(titleNames);
			createDrugFeeAndOtherFeeRateTableBody(tableDataList, table, 1, 10);
		} catch (BadElementException e) {
			LOGGER.error("BadElementException", e);
		}
		return table;
	}

	/**
	* 门诊:药品费用和其他费用---各机构对比
	* 住院:药品费用和其他费用---各机构对比
	* @param tableDataList tableDataList
	* @return table
	 * @throws BadElementException 
	*/
	public Table crateDrugFeeAndOtherFeeRateTable2(List<String[]> tableDataList, String[] titleNames) {
		Table table = null;
		try {
			table = createDrugFeeAndOtherFeeRateTableHeader(titleNames);
			createDrugFeeAndOtherFeeRateTableBody(tableDataList, table, 10, 10);
		} catch (BadElementException e) {
			LOGGER.error("BadElementException", e);
		}

		return table;
	}

	/**
	* 门诊:药品费用和其他费用---各机构对比
	* 住院:药品费用和其他费用---各机构对比
	* @param tableDataList tableDataList
	* @return table
	 * @throws BadElementException 
	*/
	public Table crateDrugFeeAndOtherFeeRateTable3(List<String[]> tableDataList, String[] titleNames) {
		Table table = null;
		try {
			table = createDrugFeeAndOtherFeeRateTableHeader(titleNames);
			createDrugFeeAndOtherFeeRateTableBody(tableDataList, table, 19, 4);
		} catch (BadElementException e) {
			LOGGER.error("BadElementException", e);
		}
		return table;
	}

	/**
	* 1. 住院费用和医保费用占比
	* 2、 公用门诊费用和医保费用占比
	*/
	private Table createTotFeeAndMedFeeRateTableHeader(String tableName) {
		Table table = null;
		try {
			table = new Table(8);
			table.setWidth(80);
			table.setWidths(new int[] { 5, 19, 12, 13, 13, 12, 13, 13 });
			table.setBackgroundColor(Color.BLACK);
			// 添加表头元素
			Cell cell = new Cell(" ");
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("5%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(" ");
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("39%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithBold(
					GeneralMethods.getInstance().addBlankForString(tableName)));
			cell.setHeader(true);
			cell.setBackgroundColor(Color.BLACK);
			// cell.setWidth("8%");
			cell.setHorizontalAlignment("Left");
			cell.setBorderWidthBottom(1);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithBold("1.2医 保 占 比  (%) "));
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("8%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			cell.setHorizontalAlignment("Left");
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setHorizontalAlignment("Left");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			table.endHeaders();

		} catch (BadElementException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		}
		return table;
	}

	/**
	* 门诊和住院：药品其他费用占比
	*/
	private Table createOtherFeeDrugTableHeader(String tableName) {
		Table table = null;
		try {
			table = new Table(6);
			table.setAlignment("Left");
			table.setBackgroundColor(Color.BLACK);
			table.setWidth(50);

			// 添加表头元素
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(" "));
			cell.setHeader(true);
			cell.setWidth("5%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("   "));
			cell.setHeader(true);
			cell.setWidth("25%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(tableName));
			cell.setHeader(true);
			cell.setWidth("10%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("本 期 占 比  (%) "));
			cell.setHeader(true);
			cell.setWidth("20%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setHeader(true);
			cell.setWidth("20%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setHeader(true);
			cell.setWidth("20%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			table.endHeaders();

		} catch (BadElementException e) {
			LOGGER.error("--createOtherFeeDrugTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		}

		return table;
	}

	/**
	* 4. 平均住院床日和每床日费用
	*/
	private Table createAvgHospAndAvgBedTableHeader(String[] titleNames) {

		Table table = null;
		try {
			table = new Table(8);
			table.setWidth(80);
			table.setWidths(new int[] { 5, 19, 12, 13, 13, 12, 13, 13 });
			table.setAlignment("Left");
			table.setBackgroundColor(Color.BLACK);
			// 添加表头元素
			Cell cell = new Cell(" ");
			cell.setHeader(true);
			// cell.setWidth("5%");
			cell.setBorderWidthBottom(0);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(" ");
			cell.setHeader(true);
			cell.setBorderWidthBottom(0);
			// cell.setWidth("25%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithBold(
					GeneralMethods.getInstance().addBlankForString(titleNames[0])));
			cell.setHeader(true);
			cell.setHorizontalAlignment("Center");
			cell.setVerticalAlignment("Center");
			cell.setColspan(3);
			// cell.setWidth("40%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidthBottom(1);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithBold(
					GeneralMethods.getInstance().addBlankForString(titleNames[1])));
			cell.setHeader(true);
			cell.setColspan(3);
			// cell.setWidth("40%");
			cell.setHorizontalAlignment("Center");
			cell.setVerticalAlignment("Center");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			table.endHeaders();

			cell = new Cell("  ");
			cell.setBorderWidthBottom(1);
			// cell.setWidth("5%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(" ");
			// cell.setWidth("39%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("本 期 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("8%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("本 期 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("8%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setBorderWidthBottom(1);
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			table.endHeaders();

		} catch (BadElementException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}

		return table;
	}

	/**
	 * 药品和其他费用table body
	 * @param tableDataList  tableDataList
	 * @param table			 table
	 * @param start			 start
	 * @throws BadElementException 
	 */
	private void createDrugFeeAndOtherFeeRateTableBody(List<String[]> tableDataList, Table table, int start, int length)
			throws BadElementException {
		if (GeneralMethods.getInstance().isEmptyCollects(tableDataList)) {
			return;
		}

		try {
			int i = 1;
			for (String[] data : tableDataList) {
				creataFirstBlankCell(table, i, tableDataList.size());
				i++;

				// 截取部分数据
				String[] subArray = getSubArray(data, start, length);

				int j = 0;
				for (String filed : subArray) {
					if (subArray.length <= 10) {

						createDrugFeeAndOtherRateTableCell(table, j, filed);

						j++;
					}
				}
			}
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}
	}

	private void createDrugFeeAndOtherRateTableCell(Table table, int j, String filed) throws BadElementException,
			DocumentException, IOException {
		if (j == 0) {
			Cell cell;
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().addBlankForString(filed) + " "));
			// cell.setWidth("19%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 1) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
			// cell.setWidth("8%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 2) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 3) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 4) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 5) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("8%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 6) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 7) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 8) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("8%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else if (j == 9) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
					GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
			// cell.setWidth("10%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		}
	}

	/**
	 * 药品这块数据量比较大，接口数据一次性返回，到界面需要截取成三个table，这里写一个公共的方法，用例截取返回的数据    
	 * @param arrays	接口返回的总数据集合
	 * @param start		要截取的气势行
	 * @param length    截取长度
	 * @return   String[] table行数据
	 */
	private String[] getSubArray(String[] arrays, int start, int length) {

		if (arrays == null) {
			return new String[0];
		}

		String[] destArray = new String[length];
		// 第一个元素是表头
		destArray[0] = arrays[0];

		// 往后cope10个元素
		System.arraycopy(arrays, start, destArray, 1, length - 1);

		return destArray;
	}

	/**
	 * 药品头 -- 第二部分：各机构对比
	 * @param titleNames 表头
	 * @return table头
	 */
	private Table createDrugFeeAndOtherFeeRateTableHeader(String[] titleNames) {
		Table table = null;
		try {
			table = new Table(titleNames.length * 3 + 2);
			table.setAlignment("Left");
			table.setWidth(9 + titleNames.length * 27);
			if (titleNames.length == 3) {
				table.setWidths(new int[] { 3, 18, 11, 8, 7, 11, 8, 7, 11, 8, 7 });

			} else if (titleNames.length == 1) {
				table.setWidths(new int[] { 3, 18, 11, 8, 7 });
			}

			table.setBackgroundColor(Color.BLACK);
			// 添加表头元素
			Cell cell = new Cell(" ");
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("5%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(" ");
			cell.setHeader(true);
			cell.setBorderWidthBottom(1);
			// cell.setWidth("19%");
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			for (String name : titleNames) {
				cell = new Cell(GeneralMethods.getInstance().createPhraseWithBold(
						GeneralMethods.getInstance().addBlankForString(name)));
				cell.setHeader(true);
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidthBottom(1);
				// cell.setWidth("8%");
				table.addCell(cell);

				cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
				cell.setHeader(true);
				cell.setBorderWidthBottom(1);
				// cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				table.addCell(cell);

				cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
				cell.setHeader(true);
				cell.setBorderWidthBottom(1);
				// cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				table.addCell(cell);
			}

			table.endHeaders();

		} catch (BadElementException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createHospFeeAndOutpatTableHeader()---", e);
		}

		return table;
	}

	private void createWordDeptsContrasTableBody(List<String[]> tableDataList, Table table) {

		if (GeneralMethods.getInstance().isEmptyCollects(tableDataList)) {
			return;
		}

		try {
			int i = 1;
			for (String[] data : tableDataList) {
				creataFirstBlankCell2(tableDataList, table, i);
				i++;

				createWordDeptsTableCell(table, data);
			}
		} catch (BadElementException e) {
			LOGGER.error("--createDrugFeeTable3Header()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}
	}

	private void createWordDeptsTableCell(Table table, String[] data) throws BadElementException, DocumentException,
			IOException {
		int j = 0;
		for (String filed : data) {
			if (data.length == 4) {
				if (j == 0) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().addBlankForString(filed) + " "));
					cell.setWidth("27%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 1) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
					cell.setWidth("20%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 2) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
					cell.setWidth("25%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 3) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
					cell.setWidth("25%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				}
				j++;
			}
		}
	}

	private void creataFirstBlankCell2(List<String[]> tableDataList, Table table, int i) throws BadElementException,
			DocumentException, IOException {
		if (i < tableDataList.size()) {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(" " + i));
			cell.setWidth("3%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		} else {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(" "));
			cell.setWidth("3%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			table.addCell(cell);
		}
	}

	private Table createWordDeptsContrasTableHeader() {

		Table table = null;
		try {
			table = new Table(5);
			table.setWidth(50);
			table.setAlignment("Left");
			table.setBackgroundColor(Color.BLACK);
			// 添加表头元素
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("     "));
			cell.setHeader(true);
			cell.setWidth("3%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("       "));
			cell.setHeader(true);
			cell.setWidth("27%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("本 期 金 额       (万 元 ) "));
			cell.setHeader(true);
			cell.setWidth("30%");
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidthBottom(1);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("同 比 "));
			cell.setHeader(true);
			cell.setWidth("20%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal("环 比 "));
			cell.setHeader(true);
			cell.setWidth("20%");
			cell.setBorderWidthBottom(1);
			cell.setBackgroundColor(Color.BLACK);
			table.addCell(cell);

			table.endHeaders();

		} catch (BadElementException e) {
			LOGGER.error("--createDrugFeeTable3Header()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}

		return table;
	}

	/**
	* 住院其他费用
	* @param tableDataList tableDataList
	* @return table
	*/
	public Table crateDrugOtherFeeTable(List<String[]> tableDataList, String tableName) {
		Table table = createOtherFeeDrugTableHeader(tableName);

		createOtherFeeForDrugTableBody(tableDataList, table);

		return table;
	}

	private void createOtherFeeForDrugTableBody(List<String[]> tableDataList, Table table) {

		if (GeneralMethods.getInstance().isEmptyCollects(tableDataList)) {
			return;
		}

		try {
			int i = 1;
			for (String[] data : tableDataList) {
				// 创建第一列的空格
				creataFirstBlankCell(table, i, tableDataList.size());
				i++;

				createOtherFeeAndDrugFeeTableCell(table, data);
			}
		} catch (BadElementException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}
	}

	private void createOtherFeeAndDrugFeeTableCell(Table table, String[] data) throws BadElementException,
			DocumentException, IOException {
		int j = 0;
		for (String filed : data) {
			if (data.length == 5) {
				if (j == 0) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed + "    "));
					cell.setWidth("25%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 1) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
					cell.setWidth("10%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 2) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
					cell.setWidth("20%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 3) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? "  " : filed + "%"));
					cell.setWidth("20%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 4) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? "   " : filed + "%"));
					cell.setWidth("20%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
				}

				j++;
			}
		}
	}

	/**
	* 门诊：门诊费用和医保费用
	* @param tableDataList
	* @return
	*/
	public Table crateOutpatAndMedTable(List<String[]> tableDataList) {
		Table table = createTotFeeAndMedFeeRateTableHeader("1.1 门 诊 费 用 ( 元 )");

		createTotFeeAndMedFeeRateTableBody(tableDataList, table);

		return table;
	}

	private void createTotFeeAndMedFeeRateTableBody(List<String[]> tableDataList, Table table) {
		if (GeneralMethods.getInstance().isEmptyCollects(tableDataList)) {
			return;
		}
		try {
			int i = 1;
			for (String[] data : tableDataList) {
				// 创建第一列的空格
				creataFirstBlankCell(table, i, tableDataList.size());
				i++;

				// 创建其他列的cell
				createTotalFeeAndMedRateTableCell(table, data);
			}
		} catch (BadElementException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (DocumentException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		} catch (IOException e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}
	}

	private void createTotalFeeAndMedRateTableCell(Table table, String[] data) throws BadElementException,
			DocumentException, IOException {
		int j = 0;
		for (String filed : data) {
			if (data.length == 7) {
				if (j == 0) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().addBlankForString(filed)));
					// cell.setWidth("39%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					table.addCell(cell);
					cell.setHorizontalAlignment("Left");
				} else if (j == 1) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
					// cell.setWidth("8%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					cell.setHorizontalAlignment("Left");
					table.addCell(cell);
				} else if (j == 2) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? " " : filed + "%"));
					// cell.setWidth("10%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setBorderWidth(0);
					cell.setHorizontalAlignment("Left");
					table.addCell(cell);
				} else if (j == 3) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? " " : filed + "%"));
					// cell.setWidth("10%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setHorizontalAlignment("Left");
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 4) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? " " : filed + "%"));
					// cell.setWidth("8%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setHorizontalAlignment("Left");
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 5) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? " " : filed + "%"));
					// cell.setWidth("10%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setHorizontalAlignment("Left");
					cell.setBorderWidth(0);
					table.addCell(cell);
				} else if (j == 6) {
					Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
							GeneralMethods.getInstance().isEmptyString(filed) ? " " : filed + "%"));
					// cell.setWidth("10%");
					cell.setBackgroundColor(Color.BLACK);
					cell.setHorizontalAlignment("Left");
					cell.setBorderWidth(0);
					table.addCell(cell);
				}

				j++;
			}
		}
	}

	private void createAvgHospAndAvgBedTableBody(List<String[]> tableDataList, Table table) {
		if (GeneralMethods.getInstance().isEmptyCollects(tableDataList)) {
			return;
		}
		try {
			int i = 1;
			for (String[] data : tableDataList) {
				// 创建第一列的空格
				creataFirstBlankCell(table, i, tableDataList.size());
				i++;

				// 创建其他列
				createAvgHostpAndAvgBedTableCell(table, data);
			}
		} catch (Exception e) {
			LOGGER.error("--createOutpatTableHeader()---", e);
		}
	}

	private void createAvgHostpAndAvgBedTableCell(Table table, String[] data) throws BadElementException,
			DocumentException, IOException {
		int j = 0;
		for (String filed : data) {
			if (data.length != 7) {
				continue;
			}

			if (j == 0) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().addBlankForString(filed) + "    "));
				cell.setWidth("39%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			} else if (j == 1) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(filed));
				cell.setWidth("8%");
				cell.setBorderWidth(0);
				cell.setBackgroundColor(Color.BLACK);
				table.addCell(cell);
			} else if (j == 2) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().isEmptyString(filed) ? "      " : filed + "%"));
				cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			} else if (j == 3) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().isEmptyString(filed) ? "      " : filed + "%"));
				cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			} else if (j == 4) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().isEmptyString(filed) ? "      " : filed));
				cell.setWidth("8%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			} else if (j == 5) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().isEmptyString(filed) ? "      " : filed + "%"));
				cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			} else if (j == 6) {
				Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(
						GeneralMethods.getInstance().isEmptyString(filed) ? "     " : filed + "%"));
				cell.setWidth("10%");
				cell.setBackgroundColor(Color.BLACK);
				cell.setBorderWidth(0);
				table.addCell(cell);
			}

			j++;
		}
	}

	private void creataFirstBlankCell(Table table, int i, int size) throws BadElementException, DocumentException,
			IOException {

		// i <= 3
		if (i < size) {
			Cell cell;
			cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(" " + i));
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			// cell.setWidth("5%");
			table.addCell(cell);
			cell.setHorizontalAlignment("Left");
		} else {
			Cell cell = new Cell(GeneralMethods.getInstance().createPhraseWithNormal(" "));
			cell.setBackgroundColor(Color.BLACK);
			cell.setBorderWidth(0);
			// cell.setWidth("5%");
			table.addCell(cell);
			cell.setHorizontalAlignment("Left");
		}
	}

	/**
	 * 转化table对应的二进制码    
	 * @param table table
	 * @param reportTemplateDir reportTemplateDir
	 * @return String
	 * 
	 */
	public String getTableStr(Table table, String reportTemplateDir) {
		Document doc = new Document();
		StringBuffer sourceText = new StringBuffer(1024);
		FileInputStream input = null;
		try {
			RtfWriter2.getInstance(doc, new FileOutputStream(this.getTmpRtfPath(reportTemplateDir)));
			doc.open();
			doc.add(table);
			doc.close();

			input = new FileInputStream(this.getTmpRtfPath(reportTemplateDir));
			byte[] b = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = input.read(b, 0, 1024)) != -1) {
				sourceText.append(new String(b, 0, bytesRead));
			}
			int startIdx = sourceText.indexOf("info}") + 5;
			int endIdx = sourceText.length() - 1;

			if (startIdx < endIdx) {
				String result = sourceText.substring(startIdx, endIdx);
				return new String(result.getBytes("utf-8"), "gb2312");
			}
		} catch (FileNotFoundException e) {
			LOGGER.error("--getTableStr FileNotFoundException---", e);
		} catch (BadElementException e) {
			LOGGER.error("--getTableStr BadElementException---", e);
		} catch (DocumentException e) {
			LOGGER.error("--getTableStr DocumentException---", e);
		} catch (IOException e) {
			LOGGER.error("--getTableStr IOException---", e);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (IOException e) {
				LOGGER.error("BadElementException", e);
			}
		}
		return "";
	}
}
